﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Kargo3504.Paneller
{
    public partial class CalismaAlaniTanimlamaFormu : UserControl
    {
        DBBaglantiDataContext baglanti = new DBBaglantiDataContext();
        public CalismaAlaniTanimlamaFormu()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void btnKaydet_Click(object sender, EventArgs e)
        {
            bool Yeni;
            Yeni = false;
            CalismaAlanlari cl;
            if (lblCalismaAlaniID.Text == "-")
            {
                Yeni = true;
                cl = new CalismaAlanlari();
            }
            else
            {
                cl = (from x in baglanti.CalismaAlanlaris where x.CalismaAlanID == Convert.ToInt32(lblCalismaAlaniID.Text) select x).SingleOrDefault();
            }
            cl.BirimID = (cmbBirimID.SelectedItem as Birimler).BirimID;
            cl.Cadde = txtCadde.Text;
            cl.CalismaAlanAciklama = txtCalismaAlaniAciklama.Text;
            cl.CalismaAlaniAktifMi = cbCalismaAlaniAktifMi.Checked;
            cl.Il = cmbIlID.SelectedItem as Il;
            cl.Ilce = cmbIlceID.SelectedItem as Ilce;
            cl.Mahalle = txtMahalle.Text;
            cl.Semt = txtSemt.Text;
            cl.Sokak = txtSokak.Text;

            if (Yeni == true)
            {
                baglanti.CalismaAlanlaris.InsertOnSubmit(cl);
            }

            baglanti.SubmitChanges();
            lblCalismaAlaniID.Text = cl.CalismaAlanID.ToString();
            MessageBox.Show("OK");

        }

        private void btnSil_Click(object sender, EventArgs e)
        {
            try
            {


                CalismaAlanlari cl = (from x in baglanti.CalismaAlanlaris where x.CalismaAlanID == Convert.ToInt32(lblCalismaAlaniID.Text) select x).SingleOrDefault();

                baglanti.CalismaAlanlaris.DeleteOnSubmit(cl);
                baglanti.SubmitChanges();
                MessageBox.Show("İlgili Kayıt Sistemden Silindi");
            }
            catch (Exception)
            {

                MessageBox.Show("Hata Oluştu Tekrar Deneyiniz");
            }

        }

        private void btnYeni_Click(object sender, EventArgs e)
        {

            cmbBirimID.SelectedIndex = -1;
            txtCadde.Text = "";
            txtCalismaAlaniAciklama.Text = "";
            cbCalismaAlaniAktifMi.Checked = false;
            cmbIlID.SelectedIndex = 0;
            txtMahalle.Text = "";
            txtSemt.Text = "";
            txtSokak.Text = "";
            lblCalismaAlaniID.Text = "-";
        }

        private void CalismaAlaniTanimlamaFormu_Load(object sender, EventArgs e)
        {
            var sorgu = (from x in baglanti.Ils orderby x.IlAdi select x).ToList();
            cmbIlID.DataSource = sorgu;
            cmbIlID.SelectedIndex = 0;

            var sorgu2 = (from x in baglanti.Birimlers select x).ToList();
            var subeler = sorgu2.FindAll(f => f.AktarmaMerkezID != null && f.BolgeID != null);
            cmbBirimID.DataSource = subeler;


        }

        private void cmbIlID_SelectedIndexChanged(object sender, EventArgs e)
        {
            var sorgu = (from x in baglanti.Ilces where x.IlID == (cmbIlID.SelectedItem as Il).IlID orderby x.IlceAdi select x).ToList();
            cmbIlceID.DataSource = sorgu;
            cmbIlceID.SelectedIndex = 0;
        }

        private void btnArama_Click(object sender, EventArgs e)
        {
            frmCalismaAlaniBul ff = new frmCalismaAlaniBul(lblCalismaAlaniID);
            DialogResult dd = ff.ShowDialog();

        }

        private void lblCalismaAlaniID_TextChanged(object sender, EventArgs e)
        {
            var sorgu = (from x in baglanti.CalismaAlanlaris where x.CalismaAlanID == Convert.ToInt32(lblCalismaAlaniID.Text) select x).SingleOrDefault();

            if (sorgu !=null)
            {
                txtCadde.Text = sorgu.Cadde;
                txtMahalle.Text = sorgu.Mahalle;
                txtSokak.Text = sorgu.Sokak;
                txtCalismaAlaniAciklama.Text = sorgu.CalismaAlanAciklama;
                txtSemt .Text = sorgu.Semt;
                cbCalismaAlaniAktifMi.Checked = sorgu.CalismaAlaniAktifMi;

                cmbBirimID.SelectedItem = sorgu.Birimler;
                cmbIlID.SelectedItem = sorgu.Il;
                cmbIlceID.SelectedItem = sorgu.Ilce;

                

            }
        }
    }
}
